/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.editpart;

import org.eclipse.uml2.uml.Association;
import org.eclipse.uml2.uml.Property;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.figures.StringFigure;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.Di2Factory;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.umlutils.ICustomAppearence;

/**
 * Relation end edit part specialized in label display (name, modifiers, etc.)
 * 
 * @author Remi Schnekenburger
 */
public class RelationEndLabelDisplayEditPart extends RelationEndDisplayEditPart {

	/**
	 * Creates a new RelationEndLabelDisplayEditPart.
	 * 
	 * @param modelManager the model manager associated to this editor
	 * @param graphnode the parent graphnode
	 */
	public RelationEndLabelDisplayEditPart(GraphNode graphnode, ModelManager modelManager) {
		super(graphnode, modelManager);
	}

	/**
	 * Creates a new RelationEndLabelDisplayEditPart.
	 * 
	 * @param element the association "parent" of this relation end
	 * @param multiplicity 	 * @param modelManager the model manager associated to this editor
	 * @param end boolean that indicates if it is near the source  (<code>false</code>)or the target (<code>true</code>)
	 * @param graphnode the parent graphnode
	 */
	public RelationEndLabelDisplayEditPart(GraphNode graphnode, Association element, boolean end, ModelManager modelManager) {
		super(graphnode, element, end, false, modelManager);
	}


	/**
	 * Retrieves the property associated to this edit part.
	 * 
	 * @return the property associated to this edit part
	 */
	protected Property getProperty() {
		if (element.getMemberEnds().size()>1){
			if (isAtEnd()){
				return (Property) element.getMemberEnds().get(0);
			}
			else {
				return (Property) element.getMemberEnds().get(1);
			}
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.editpart.NameDisplayEditPart#createFigure()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	protected StringFigure createFigure() {
		com.cea.papyrus.umlutils.Property utilProperty = new com.cea.papyrus.umlutils.Property(getProperty());

		// get text to be displayed
		final String newText = getText(utilProperty);

		// creates and return the figure
		return new StringFigure(newText);
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.editpart.NameDisplayEditPart#refreshVisuals()
	 */
	/**
	 * 
	 */
	@Override
	protected void refreshVisuals() {
		final StringFigure f = (StringFigure) getFigure();

		if( getProperty()!=null){
			// retrieve associated property
			com.cea.papyrus.umlutils.Property utilProperty = new com.cea.papyrus.umlutils.Property(getProperty());

			// retrieve string corresponding to its multiplicity
			final String newText = getText(utilProperty);
			f.setText(newText);

			f.setVisible(this.isVisible());
		}

//		if(this.isVisible()) {
//		f.setUnactive();
//		} else {
//		f.setActive();
//		}

		/*
		// StringFigure f = getFigure();
		// Dimension size = getGraphNode().getSize();
		// Point p = getGraphNode().getPosition();
		// Rectangle r = new Rectangle(p ,size);
		if (isAtEnd()){
			if( !displayMultiplicity){
				(this.getFigure()).setText(displayProperty(((Property) element.getMemberEnds().get(0))));
			}
			else{

				int min=((LiteralInteger)((Property) element.getMemberEnds().get(0)).getLowerValue()).getValue();
				int max=((LiteralUnlimitedNatural)((Property) element.getMemberEnds().get(0)).getUpperValue()).getValue();
				(this.getFigure()).setText(getMultiplicity(min,max));
			}
		}
		else{
			if( !displayMultiplicity){
				(this.getFigure()).setText(displayProperty(((Property) element.getMemberEnds().get(1))));
			}
			else{
				int min=((LiteralInteger)((Property) element.getMemberEnds().get(1)).getLowerValue()).getValue();
				int max=((LiteralUnlimitedNatural)((Property) element.getMemberEnds().get(1)).getUpperValue()).getValue();
				(this.getFigure()).setText(getMultiplicity(min,max));

			}
		}

		if(this.isVisible()) {
			(this.getFigure()).setUnactive();
		} else {
			(this.getFigure()).setActive();
		}
		 */
	}

	/**
	 * Returns the text that should be displayed in the label for the given
	 * property.
	 * 
	 * @param utilProperty the property to display label
	 * 
	 * @return the text to be displayed
	 */
	private String getText(com.cea.papyrus.umlutils.Property utilProperty) {
		// default label
		int style = ICustomAppearence.DEFAULT_UML_RELATIONEND_PROPERTY;

		// get properties on graph node for property: custom display
		String value = getProperty(ICustomAppearence.CUSTOM_APPEARENCE_KEY);
		if(value == null) {
			com.cea.papyrus.diagraminterchange2.di2.Property prop = Di2Factory.eINSTANCE.createProperty();
			getGraphNode().getProperty().add(prop);
			prop.setKey(ICustomAppearence.CUSTOM_APPEARENCE_KEY);
			value = ""+ICustomAppearence.DEFAULT_UML_RELATIONEND_PROPERTY;
			prop.setValue(value);
		}

		// now, value should no be null
		if(value != null) {
			try {
				style = Integer.parseInt(value);
			} catch (NumberFormatException e) {
				Debug.log(e);
			}
		}

		return utilProperty.getCustomLabel(style);
	}
}
